﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DataContract(Name = "mediaItem", Namespace = "")]
    public class MediaItem : IStateManager
    {
        #region Fields

        bool defaultValue = false;
        private bool isTrackingViewStateValue;
        private StateBag viewStateValue;

        #endregion Fields

        #region Constructors

        public MediaItem()
        {
            // overloaded constructor
        }

        public MediaItem(string title, string url, string value)
        {
            Title = title;
            Url = url;
            Value = value;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// When true, which is also the default, this image will preload in the back. For this setting
        /// to take effect however, PreloadAll needs to be true on the MediaFrame control. By default, 
        /// PreloadAll is false, which overrides this setting.
        /// </summary>
        [DataMember(Name="preload"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(true),
        NotifyParentProperty(true),
        Description("Image gets preloaded in background when true")]
        public bool Preload
        {
            get
            {
                object o = (object)ViewState["Preload"];
                return (o == null) ? true : (bool)o;
            }
            set
            {
                defaultValue = false;
                ViewState["Preload"] = Preload;
            }
        }

        /// <summary>
        /// The preload priority for this mediaItem. Images can be preloaded in the background
        /// while the user is viewing the current image
        /// </summary>
        [DataMember(Name="priority"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(0),
        NotifyParentProperty(true),
        Description("Gets or Sets the load Priority")]
        public int Priority
        {
            get
            {
                object o = (object)ViewState["Priority"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                defaultValue = false;
                ViewState["Priority"] = Priority;
            }
        }

        [DataMember(Name="title"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(""),
        NotifyParentProperty(true),
        Description("Gets or Sets the Title")]
        public string Title
        {
            get
            {
                object o = (object)ViewState["Title"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["Title"] = value;
            }
        }

        [DataMember(Name="url"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(""),
        NotifyParentProperty(true),
        Description("Gets or Sets the Url"),
        Editor(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor))]
        public string Url
        {
            get
            {
                object o = (object)ViewState["Url"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["Url"] = value;
            }
        }

        [DataMember(Name="value"),
        Bindable(false),
        Category("Behavior"),
        DefaultValue(""),
        NotifyParentProperty(true),
        Description("Gets or Sets the Value")]
        public string Value
        {
            get
            {
                object o = (object)ViewState["Value"];
                return (o == null) ? string.Empty : o.ToString();
            }
            set
            {
                defaultValue = false;
                ViewState["Value"] = value;
            }
        }

        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return isTrackingViewStateValue;
            }
        }

        internal bool Defaults
        {
            get
            {
                return defaultValue;
            }
            set
            {
                defaultValue = value;
            }
        }

        private StateBag ViewState
        {
            get
            {
                if (viewStateValue == null)
                {
                    viewStateValue = new StateBag(false);
                    if (isTrackingViewStateValue)
                    {
                        ((IStateManager)viewStateValue).TrackViewState();
                    }
                }
                return viewStateValue;
            }
        }

        #endregion Properties

        #region Methods

        void IStateManager.LoadViewState(object savedState)
        {
            this.LoadViewState(savedState);
        }

        object IStateManager.SaveViewState()
        {
            if (Defaults)
                return null;
            return this.SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            isTrackingViewStateValue = true;
            if (viewStateValue != null)
                ((IStateManager)viewStateValue).TrackViewState();
        }

        internal void LoadViewState(object savedState)
        {
            object[] states = (object[])savedState;
            if (states != null)
            {
                ((IStateManager)ViewState).LoadViewState(states[0]);
            }
        }

        internal object SaveViewState()
        {
            object[] states = new object[1];
            states[0] = (viewStateValue != null) ? ((IStateManager)viewStateValue).SaveViewState() : null;
            return states;
        }

        internal void SetDirty()
        {
            viewStateValue.SetDirty(true);
        }

        #endregion Methods
    }
}